package com.qf.admin.service.system.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.admin.domain.form.UserForm;
import com.qf.common.domain.admin.dos.SysUser;
import com.qf.common.domain.admin.vo.UserPageVO;
import com.qf.admin.service.system.SysUserRoleService;
import com.qf.admin.service.system.SysUserService;
import com.qf.common.constant.CommonConstant;
import com.qf.common.core.domain.ResponseCode;
import com.qf.common.domain.admin.dos.SysUserRole;
import com.qf.common.exception.BaseException;
import com.qf.common.domain.admin.mapper.SysUserMapper;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
* @author sin
* @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
* @createDate 2024-10-12 16:52:40
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    SysUserRoleService userRoleService;

    @Resource
    PasswordEncoder passwordEncoder;

    @Override
    public SysUser selectUserByUsername(String username) {
        return sysUserMapper.selectByUsername(username);
    }

    @Override
    public List<UserPageVO> getUserPage(String username, String mobile, Integer status) {
        try {
            return sysUserMapper.queryUser(username, mobile, status);
        } catch (Exception e) {
            throw new BaseException(ResponseCode.OPT_ERROR.getCode(), CommonConstant.SELECT + CommonConstant.USER + CommonConstant.ERROR);
        }
    }

    @Override
    public boolean updateUser(UserForm userForm) {
        try {
            SysUser user = sysUserMapper.selectById(userForm.getId());
            if (userForm.getRoleId() != null)
                userRoleService.updateById(new SysUserRole(userForm.getId(), userForm.getRoleId()));
            return sysUserMapper.updateById(getUser(user, userForm)) > 0;
        } catch (Exception e) {
            throw new BaseException(ResponseCode.OPT_ERROR.getCode(), userForm.getUsername() + CommonConstant.EDIT + CommonConstant.USER + CommonConstant.ERROR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUser(UserForm userForm) {
        SysUser user = sysUserMapper.getUserByUsername(userForm.getUsername());
        if(user != null) throw new BaseException(ResponseCode.ERROR.getCode(), CommonConstant.USER + CommonConstant.EXIST);
        userForm.setPassword(passwordEncoder.encode(userForm.getPassword()));
        user = getUser(new SysUser(),userForm);
        try {
            this.save(user);
            return userRoleService.save(new SysUserRole(user.getId(), userForm.getRoleId()));
        } catch (Exception e) {
            throw new BaseException(ResponseCode.OPT_ERROR.getCode(), userForm.getUsername() + CommonConstant.ADD + CommonConstant.USER + CommonConstant.ERROR);
        }
    }

    @Override
    public boolean uploadAvatar(MultipartFile avatar, Long userId) {
        // TODO 上传文件到云服务器，返回url
        String url = "";
        try {
            return sysUserMapper.uploadAvatar(url, userId);
        } catch (Exception e) {
            throw new BaseException(ResponseCode.OPT_ERROR.getCode(), CommonConstant.EDIT + CommonConstant.USER + CommonConstant.ERROR);
        }
    }

    @Override
    public boolean resetPassword(SysUser user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return sysUserMapper.updateById(user) > 0;
    }

    // 获取用户信息,并设置修改用户或者新增用户
    private SysUser getUser(SysUser user, UserForm userForm) {
        // 修改用户信息，需要修改角色信息
        user.setNickname(userForm.getNickname());
        user.setEmail(userForm.getEmail());
        user.setMobile(userForm.getMobile());
        user.setStatus(userForm.getStatus());
        user.setRemark(userForm.getRemark());
        user.setUsername(userForm.getUsername());
        user.setGender(userForm.getGender());
        user.setPassword(userForm.getPassword());
        return user;
    }
}






